{include file="public/head"/}
<link rel="stylesheet" href="__PUBLIC__phone/css/passwd.css"/>
<!-- switch按钮样式 -->
<link rel="stylesheet" href="http://www.bootcss.com/p/bootstrap-switch/static/stylesheets/bootstrapSwitch.css"/>
<!-- 地区样式 -->
<link rel="stylesheet" href="__PUBLIC__phone/picker/css/picker.css"/>
<body>
<!--头部-->
<header id="headers">
    <div class="container">
        <div class="row">
            <div class="col-xs-12">
                <a href="javascript:;" class="leftRuted glyphicon glyphicon-menu-left pull-left" ></a>
                <p>添加收货地址</p>
            </div>
        </div>
    </div>
</header>
<!-- 个人资料 -->
<div class="personData normalBox">
    <div class="normarWrap orderWrap container">
        <form id="form">
            <div class="form-group">
                <label for="consignee">收件人：</label>
                <input type="text" id="consignee" name="consignee" placeholder="请输入收件人" maxlength="10" class="form-control"/>
            </div>
            <div class="form-group">
                <label for="phone">联系电话：</label>
                <input type="text" id="phone" name="phone" placeholder="请输入联系电话" maxlength="11" class="form-control"/>
            </div>
            <div class="form-group">
                <label for="location">所在地区：</label>
                <input type="text" style="background: white;" placeholder="请选择所在地区" readonly id="location" name="location" class="form-control">
                <input type="hidden" id="regions" name="regions" class="form-control"/>
            </div>
            <div class="form-group">
                <label for="detailed">详细地址：</label>
                <input type="text" id="detailed" name="detailed" placeholder="请输入收件的详细地址" maxlength="50" class="form-control"/>
            </div>
            <div class="form-group">
                <label for="detailed">默认地址：</label>
                <div class="switch switch-small" style="margin-bottom: -5px;border-radius: 18px;margin-left: 12px;" data-on-label="是" data-off-label="否">
                    <input type="checkbox" name="type" checked />
                </div>
            </div>
            <div class="pwdWrap">
                <p>错误信息</p>
            </div>
            <div class="form-group subwrap">
                <button type="button" class="btn submitbtn">添加</button>
            </div>
        </form>
    </div>
</div>
<script src="__PUBLIC__/phone/picker/js/regions.js"></script>
<script src="__PUBLIC__/phone/picker/js/picker.js"></script>
<script src="http://www.bootcss.com/p/bootstrap-switch/static/js/bootstrapSwitch.js"></script>
<script type="text/javascript">
    var area1 = new LArea();
    area1.init({
        'trigger': '#location',
        'valueTo': '#regions',
        'keys': {
            id: 'id',
            name: 'name'
        },
        'type': 1,
        'data': Regions
    });
    area1.value=[0,0,0]; // 控制初始位置

    $(function(){
        // 添加地址
        $('button.submitbtn').on('click',function(){
            var fData = $('#form').serializeArray();
            for(var i in fData){
                if(fData[i]['name'] == 'location' ||  fData[i]['name'] == 'type'){  // 不需要验证
                    continue;
                }
                var fText = $('input[name="'+fData[i]['name']+'"]').attr('placeholder');
                if(fData[i]['name'] == 'phone'){  // 验证手机号
                    if(!/^1[3|4|5|7|8][0-9]{9}$/.test(fData[i]['value'])){
                        $('.pwdWrap p').text('请输入正确的手机号');
                        $('.pwdWrap').show();
                        return false;
                    }
                }
                if(!$.trim(fData[i]['value'])){  // 表单信息不完整
                    $('.pwdWrap p').text(fText);
                    $('.pwdWrap').show();
                    return false;
                }
            }
            // 添加
            $.post("{:url('/home/Personal/addAddress')}",fData,function(data){
                if(data.code == '0'){
                    reminder(data.msg,2000,function(){
                        self.location = document.referrer;   // 返回上一页并刷新
                    });
                } else {
                    reminder(data.msg);
                }
            })
        })
    })

</script>
</body>
</html>